/**
 * @author qiushaohua 2012-3-19
 */
package com.feizhi.common.convert;



/**
 * @author qiushaohua 2012-3-19
 * @version 0.0.1
 */
public class Converter {

    /**
     * Try to convert the object to a string if possible, otherwise return an empty string ("").
     * 
     * @param object
     * @return
     * @author qiushaohua 2012-4-4
     */
    public static String toString(Object object) {
        return toString(object, "");
    }

    /**
     * Try to convert the object to a primitive int value if possible, otherwise return the default value.
     * 
     * @param object
     * @param defaultValue
     * @return
     * @author qiushaohua 2012-4-4
     */
    public static String toString(Object object, String defaultValue) {
        if (object == null) {
            return defaultValue;
        }
        return object.toString().trim();
    }

    /**
     * Try to convert the object to a primitive int value if possible, otherwise return 0.
     * 
     * @param object
     * @return
     * @author qiushaohua 2012-4-4
     */
    public static int toInt(Object object) {
        return toInt(object, 0);
    }

    /**
     * Try to convert the object to a primitive int value if possible, otherwise return the default value.
     * 
     * @param object
     * @param defaultValue
     * @return
     * @author qiushaohua 2012-4-4
     */
    public static int toInt(Object object, int defaultValue) {
        if (object == null) {
            return defaultValue;
        }

        if (object instanceof Number) {
            return ((Number) object).intValue();
        }

        String str = object.toString().trim();
		try {
			return Integer.parseInt(str);
        } catch (Exception e) {
        }

        try {
			return (int) Long.parseLong(str);
        } catch (Exception e) {
        }

        try {
            return (int) Double.parseDouble(str);
        } catch (NumberFormatException e) {
        }

        return defaultValue;
    }

    /**
     * Try to convert the object to a primitive long value if possible, otherwise return 0.
     * 
     * @param object
     * @return
     * @author qiushaohua 2012-4-4
     */
    public static long toLong(Object object) {
        return toLong(object, 0);
    }

    /**
     * Try to convert the object to a primitive long value if possible, otherwise return the default value.
     * 
     * @param object
     * @param defaultValue
     * @return
     * @author qiushaohua 2012-4-4
     */
    public static long toLong(Object object, long defaultValue) {
        if (object == null) {
            return defaultValue;
        }

        if (object instanceof Number) {
            return ((Number) object).longValue();
        }

        String str = object.toString().trim();
		try {
			return Long.parseLong(str);
        } catch (Exception e) {
        }

        try {
            return (long) Double.parseDouble(str);
        } catch (NumberFormatException e) {
        }

        return defaultValue;
    }

    /**
     * Try to convert the object to a primitive double value if possible, otherwise return 0.0.
     * 
     * @param object
     * @return
     * @author qiushaohua 2012-4-4
     */
    public static double toDouble(Object object) {
        return toDouble(object, 0.0);
    }

    /**
     * Try to convert the object to a primitive double value if possible, otherwise return the default value.
     * 
     * @param object
     * @param defaultValue
     * @return
     * @author qiushaohua 2012-4-4
     */
    public static double toDouble(Object object, double defaultValue) {
        if (object == null) {
            return defaultValue;
        }

        if (object instanceof Number) {
            return ((Number) object).doubleValue();
        }

        try {
            return Double.parseDouble(object.toString().trim());
        } catch (NumberFormatException e) {
        }

        return defaultValue;
    }

	public static String arrayToString(Object[] arr, Transfer<Object> transfer) {
		return arrayToString(arr, ",", transfer);
	}

	public static String arrayToString(Object[] arr, String delim,
			Transfer<Object> transfer) {

		if (arr == null || arr.length == 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < arr.length; i++) {
			if (i > 0) {
				sb.append(delim);
			}
			Object obj = arr[i];
			if (transfer != null) {
				obj = transfer.transfer(obj);
			}
			sb.append(obj);
		}
		return sb.toString();
	}
}
